﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using QLCONGNOLIBS.Securities.DungDD_SecurityFramework;
using QLCONGNOLIBS.Ultils;

namespace QLCONGNOLIBS.Models.DungDD_Models
{
    public class IDepartment_Model_DungDD
    {
        //1. Datafield
        private SqlConnection objConnection_g;
        private SqlCommand objSqlCommand_g;
        private SqlDataAdapter objSqlApAdapter_g;
        private DataTable objDataTable_g;
        private tblUser_SC objUserSc_g;

        //2. Khai báo các thủ tục sẽ sử dụng

        private const string SELECT_DEPARTMENT_ACTIVE = "DE_SP_SelectIDepartmentAll_DungDD";
        private const string SELECT_DEPARTMENT_NOTSET = "DE_SP_SelectDepartment_NotSet";
        private const string SELECT_DEPARTMENT_DELETED = "DE_SP_SelectDepartment_Deleted";
        private const string CHECKDUPLICATE_DEPARTMENT_NAME = "DE_SP_CheckDuplicate_DepartmentName";
        private const string INSERT_UPDATE_DEPARTMENT = "DE_SP_InsertUpdateIDepartment_DungDD";
        private const string UPDATE_NOTE_DEPARTMENT = "DE_SP_UpdateNoteDepartment_DungDD";
        private const string UPDATE_DEPARTMENT_STATUS = "DE_SP_UpdateDepartmentRecord_Status";
        private const string DELETE_DEPARTMENT_BYID = "DE_SP_DeleteDepartment_record";
        //3. Hàm khởi dựng - Constructors
        public IDepartment_Model_DungDD()
        {

        }
        public IDepartment_Model_DungDD(SqlConnection objConnectionPara, tblUser_SC objUserP)
        {
            //Thiết lập thông tin kết nối cho datafield
            objConnection_g = objConnectionPara;
            objUserSc_g = objUserP;
        }
        /// <summary>
        /// GetDepartmentTable
        /// </summary>
        /// <returns></returns>
        public DataTable GetDepartment_All()
        {
            DataTable objDataTable = new DataTable();
            try
            {
                objSqlCommand_g = new SqlCommand(SELECT_DEPARTMENT_ACTIVE, objConnection_g);
                objSqlCommand_g.CommandType = CommandType.StoredProcedure;
                objSqlApAdapter_g = new SqlDataAdapter(objSqlCommand_g);
                objSqlApAdapter_g.Fill(objDataTable);
            }
            catch (Exception)
            {
                throw;
            }
            return objDataTable;
        }

        public DataTable GetDepartment_NotSet()
        {
            DataTable objDataTable = new DataTable();
            try
            {
                objSqlCommand_g = new SqlCommand(SELECT_DEPARTMENT_NOTSET, objConnection_g);
                objSqlCommand_g.CommandType = CommandType.StoredProcedure;
                objSqlApAdapter_g = new SqlDataAdapter(objSqlCommand_g);
                objSqlApAdapter_g.Fill(objDataTable);
            }
            catch (Exception)
            {
                throw;
            }
            return objDataTable;
        }

        public DataTable GetDepartment_deleted()
        {
            DataTable objDataTable = new DataTable();
            try
            {
                objSqlCommand_g = new SqlCommand(SELECT_DEPARTMENT_DELETED, objConnection_g);
                objSqlCommand_g.CommandType = CommandType.StoredProcedure;
                objSqlApAdapter_g = new SqlDataAdapter(objSqlCommand_g);
                objSqlApAdapter_g.Fill(objDataTable);
            }
            catch (Exception)
            {
                throw;
            }
            return objDataTable;
        }
        /// <summary>
        /// check duplicate department Name
        /// </summary>
        /// <param name="DName"></param>
        /// <returns></returns>
        public bool IsDuplicate_DepartmentName_M(string DName)
        {
            DataTable objTable;
            try
            {
                SqlParameter[] param = new SqlParameter[1];
                param[0] = (new BUltils()).CreateParammeter("@DepartmentName", DName);
                //Thực thi truy vấn thông qua Store Procedure
                objTable = new DataTable();
                BUltils obj = new BUltils(objConnection_g);
                objTable = obj.ExecProcReturnTable(CHECKDUPLICATE_DEPARTMENT_NAME, param);

                if (objTable.Rows.Count > 0)
                {
                    return true;
                }
            }
            catch (Exception)
            {
                throw;
            }
            return false;
        }

        public bool Insert_Update_Department_m(IDepartment_Entity objEntity)
        {
            try
            {
                SqlParameter[] param = new SqlParameter[4];
                param[0] = (new BUltils()).CreateParammeter("@DepartmentID", objEntity.DepartmentIdP);
                param[1] = (new BUltils()).CreateParammeter("@DepartmentName", objEntity.DepartmentName);
                param[2] = (new BUltils()).CreateParammeter("@isDeleted", objEntity.isDeleted);
                param[3] = (new BUltils()).CreateParammeter("@Note", objEntity.Note);
                BUltils obj = new BUltils(objConnection_g);
                obj.ExecProcNoReturn(INSERT_UPDATE_DEPARTMENT, param);
            }
            catch (Exception)
            {
                throw;
                return false;
            }
            return true;
        }

        public bool UpdateDepartmentStatus_ByID_m(Guid id, bool isDeleted)
        {
            try
            {
                SqlParameter[] param = new SqlParameter[2];
                param[0] = (new BUltils()).CreateParammeter("@DepartmentID", id);
                param[1] = (new BUltils()).CreateParammeter("@isDeleted", isDeleted);
                BUltils obj = new BUltils(objConnection_g);
                obj.ExecProcNoReturn(UPDATE_DEPARTMENT_STATUS, param);
            }
            catch (Exception)
            {
                throw;
                return false;
            }
            return true;
        }

        public bool DeleteDepartment_ByID_m(Guid objGuid)
        {
            try
            {
                SqlParameter[] param = new SqlParameter[1];
                param[0] = (new BUltils()).CreateParammeter("@DepartmentID", objGuid);
                BUltils obj = new BUltils(objConnection_g);
                obj.ExecProcNoReturn(DELETE_DEPARTMENT_BYID, param);
            }
            catch (Exception)
            {
                throw;
                return false;
            }
            return true;
        }
        /// <summary>
        /// Cập nhật nhanh thông tin ghi chú
        /// </summary>
        /// <param name="departmentID"></param>
        /// <param name="note"></param>
        /// <returns></returns>
        public bool Update_note_department_m(Guid departmentID, string note)
        {
            try
            {
                SqlParameter[] param = new SqlParameter[2];
                param[0] = (new BUltils()).CreateParammeter("@DepartmentID", departmentID);
                param[1] = (new BUltils()).CreateParammeter("@Note", note);
                BUltils obj = new BUltils(objConnection_g);
                obj.ExecProcNoReturn(UPDATE_NOTE_DEPARTMENT, param);
            }
            catch (Exception)
            {

                throw;
                return false;
            }
            return true;
        }

    }
}
